package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ILogBusinessUserService;
import com.mdd.admin.validate.LogBusinessUserCreateValidate;
import com.mdd.admin.validate.LogBusinessUserSearchValidate;
import com.mdd.admin.validate.LogBusinessUserUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LogBusinessUserDetailVo;
import com.mdd.admin.vo.LogBusinessUserListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LogBusinessUser;
import com.mdd.common.mapper.LogBusinessUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 商户后台日志实现类
 * @author LikeAdmin
 */
@Service
public class LogBusinessUserServiceImpl implements ILogBusinessUserService {
        
    @Resource
    LogBusinessUserMapper logBusinessUserMapper;

    /**
     * 商户后台日志列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LogBusinessUserListedVo>
     */
    @Override
    public PageResult<LogBusinessUserListedVo> list(PageValidate pageValidate, LogBusinessUserSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LogBusinessUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        logBusinessUserMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:account:str",
            "=:logStatus@log_status:int",
            "=:content:str",
        });

        IPage<LogBusinessUser> iPage = logBusinessUserMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LogBusinessUserListedVo> list = new LinkedList<>();
        for(LogBusinessUser item : iPage.getRecords()) {
            LogBusinessUserListedVo vo = new LogBusinessUserListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 商户后台日志详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LogBusinessUser
     */
    @Override
    public LogBusinessUserDetailVo detail(Integer id) {
        LogBusinessUser model = logBusinessUserMapper.selectOne(
                new QueryWrapper<LogBusinessUser>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LogBusinessUserDetailVo vo = new LogBusinessUserDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 商户后台日志新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LogBusinessUserCreateValidate createValidate) {
        LogBusinessUser model = new LogBusinessUser();
        model.setAccount(createValidate.getAccount());
        model.setLogStatus(createValidate.getLogStatus());
        model.setContent(createValidate.getContent());
        model.setCreateTime(new Date());
        logBusinessUserMapper.insert(model);
    }

    /**
     * 商户后台日志编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LogBusinessUserUpdateValidate updateValidate) {
        LogBusinessUser model = logBusinessUserMapper.selectOne(
                new QueryWrapper<LogBusinessUser>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setAccount(updateValidate.getAccount());
        model.setLogStatus(updateValidate.getLogStatus());
        model.setContent(updateValidate.getContent());
        logBusinessUserMapper.updateById(model);
    }

    /**
     * 商户后台日志删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LogBusinessUser model = logBusinessUserMapper.selectOne(
                new QueryWrapper<LogBusinessUser>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        logBusinessUserMapper.delete(new QueryWrapper<LogBusinessUser>().eq("id", id));
    }

}
